package com.ruoyi.productCheck.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateCleanRecord;
import com.ruoyi.mateproduction.domain.MtCourseProject;
import com.ruoyi.mateproduction.domain.MtCourseSample;
import com.ruoyi.mateproduction.mapper.MateCleanRecordMapper;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.productCheck.domain.CheckInformation;
import com.ruoyi.productCheck.domain.CheckInformationModel;
import com.ruoyi.productCheck.domain.CheckSample;
import com.ruoyi.productCheck.mapper.CheckInformationMapper;
import com.ruoyi.productCheck.service.CheckInformationService;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class CheckInformationServiceImpl implements CheckInformationService {

    @Autowired
    CheckInformationMapper checkInformationMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateCleanRecordMapper mateCleanRecordMapper;

    @Override
    public int addInformation(CheckInformation checkInformation) {
        int num = 0;
        Map<String, Object> resMap = new HashMap<>();
        try {
            num = checkInformationMapper.addInformation(checkInformation);
            if (checkInformation.getCheckInformationModels() != null) {
                for (CheckInformationModel checkInformationModel : checkInformation.getCheckInformationModels()) {
                    checkInformationModel.setDeptId(SecurityUtils.getDeptId());
                    checkInformationModel.setCheckId(checkInformation.getCheckId());
                    checkInformationMapper.addInformationModel(checkInformationModel);
                }
                MtAudit mtAudit = new MtAudit();
                mtAudit.setCompanyId(SecurityUtils.getDeptId());
                mtAudit.setOrderId(checkInformation.getCheckId());
                mtAudit.setOrderType("13");
                mtAudit.setOrderNum(checkInformation.getCheckOrderNum());
                mtAudit.setAuditStatus("0");
                mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit);
                MateCleanRecord mateCleanRecord = new MateCleanRecord();
                mateCleanRecord.setCleanId(checkInformation.getCleanId());
                mateCleanRecord.setIsCreatDaily("1");
                mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);
            }
        } catch (Exception e) {
            resMap.put("orderId", checkInformation.getCheckId());
            resMap.put("orderType", "13");
            mtAuditMapper.deleteMtAuditById(resMap);
            checkInformationMapper.removeCheckInformation(checkInformation.getCheckId());
            checkInformationMapper.removeCheckInformationModel(checkInformation.getCheckId());
            MateCleanRecord mateCleanRecord = new MateCleanRecord();
            mateCleanRecord.setCleanId(checkInformation.getCleanId());
            mateCleanRecord.setIsCreatDaily("0");
            mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);
        }
        return num;
    }

    @Override
    public List<CheckInformation> informationList(CheckInformation checkInformation) {
        return checkInformationMapper.informationList(checkInformation);
    }

    @Override
    public CheckInformation selectInformation(Long checkId) {
        return checkInformationMapper.selectInformation(checkId);
    }

    @Override
    public void remove(Long checkId) {
        checkInformationMapper.removeCheckInformation(checkId);
        checkInformationMapper.removeCheckInformationModel(checkId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", checkId);
        resMap.put("orderType", "13");
        mtAuditMapper.deleteMtAuditById(resMap);
        CheckInformation checkInformation = checkInformationMapper.selectInformation(checkId);
      /*  MateCleanRecord mateCleanRecord = new MateCleanRecord();
        mateCleanRecord.setDeptId(SecurityUtils.getDeptId());
        mateCleanRecord.setCleanId(checkInformation.getCleanId());
        mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);*/
    }

    @Override
    public int updateInformation(CheckInformation checkInformation) {
        int num = 0;
        if (checkInformation.getCheckInformationModels() != null) {
            checkInformationMapper.removeCheckInformationModel(checkInformation.getCheckId());
            for (CheckInformationModel checkInformationModel : checkInformation.getCheckInformationModels()) {
                checkInformationModel.setCheckId(checkInformation.getCheckId());
                checkInformationModel.setDeptId(SecurityUtils.getDeptId());
                checkInformationMapper.addInformationModel(checkInformationModel);
            }
            num = checkInformationMapper.updateInformation(checkInformation);

        }
        return num;
    }

    @Override
    public List<CheckInformation> auditInformation(Long deptId) {
        return checkInformationMapper.auditInformation(deptId);
    }

    @Override
    public String createWord(Long checkId) {
        int num = 0;
        try {
            XWPFDocument document = new XWPFDocument();
            List<CheckSample> list = checkInformationMapper.selectCheckSample(checkId);
            List<CheckInformationModel> parameters = checkInformationMapper.selectCheckInformationModel(checkId);
            if (parameters.size() == 0) {
                num = 1;
            }

            if (num == 1) {
                return "生成失败";
            } else {
                XWPFParagraph paragraph = document.createParagraph();
                paragraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun run = paragraph.createRun();
                run.setText("附件：成品检验结果");
                run.setFontSize(16);
                run.setBold(true);
                run.addBreak();
// 创建一个表格
                XWPFTable table = document.createTable(list.size() + 1, parameters.size() + 2); // 调整行数加1
                CTTblPr tblPr = table.getCTTbl().addNewTblPr();

                tblPr.addNewTblW().setW(BigInteger.valueOf(10000)); // 设置表格宽度
                for (int i = 0; i < list.size() + 1; i++) { // 调整循环次数
                    XWPFTableRow row = table.getRow(i);
                    row.setHeight(1); // 设置行高
                    for (int j = 0; j < parameters.size() + 2; j++) {
                        XWPFTableCell cell = row.getCell(j);
                        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); // 垂直居中对齐
                        cell.setWidth("100"); // 设置列宽
                    }
                }

                XWPFTableCell cellHeader = table.getRow(0).getCell(0);
                cellHeader.setText("样本");
                cellHeader.getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER);
                cellHeader.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);

// 填充第一列除第一行
                for (int i = 1; i < list.size() + 1; i++) {
                    XWPFTableCell cell = table.getRow(i).getCell(0);
                    cell.setText(list.get(i - 1).getSampleName());
                    CTP ctp = cell.getCTTc().getPList().get(0);
                    CTPPr ctpPr = ctp.getPPr();
                    if (ctpPr == null) {
                        ctpPr = ctp.addNewPPr();
                    }
                    CTJc jc = ctpPr.getJc();
                    if (jc == null) {
                        jc = ctpPr.addNewJc();
                    }
                    jc.setVal(STJc.CENTER);
                }

// 填充第一行除第一列的数据
                for (int j = 1; j < parameters.size() + 1; j++) {
                    XWPFTableCell cell = table.getRow(0).getCell(j);
                    cell.setText(parameters.get(j - 1).getCheckProject());
                    CTP ctp = cell.getCTTc().getPList().get(0);
                    CTPPr ctpPr = ctp.getPPr();
                    if (ctpPr == null) {
                        ctpPr = ctp.addNewPPr();
                    }
                    CTJc jc = ctpPr.getJc();
                    if (jc == null) {
                        jc = ctpPr.addNewJc();
                    }
                    jc.setVal(STJc.CENTER);
                }

// 填充除第一行第一列外的数据
                for (int i = 1; i < list.size() + 1; i++) { // 修改起始行数
                    for (int j = 1; j < parameters.size() + 1; j++) {
                        XWPFTableCell cellData = table.getRow(i).getCell(j);
                        cellData.setText(parameters.get(j - 1).getCheckResult());

                        CTP ctp = cellData.getCTTc().getPList().get(0);     
                        CTPPr ctpPr = ctp.getPPr();
                        if (ctpPr == null) {
                            ctpPr = ctp.addNewPPr();
                        }
                        CTJc jc = ctpPr.getJc();
                        if (jc == null) {
                            jc = ctpPr.addNewJc();
                        }
                        jc.setVal(STJc.CENTER);

                    }
                }

                for (int i = 0; i < list.size() + 1; i++) { // 修改循环次数
                    XWPFTableCell cellDecide = table.getRow(i).getCell(parameters.size() + 1);
                    if (i == 0) {
                        cellDecide.setText("判定");
                        cellDecide.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
                        cellDecide.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER); // 判定表头居中对齐

                    } else {
                        cellDecide.setText(parameters.get(i - 1 < parameters.size() ? i - 1 : parameters.size() - 1).getCheckConclusion());
                        cellDecide.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER); // 内容居中对齐
                    }
                }

                // 保存文档
                String filePath = "D:\\ruoyi\\uploadPath\\download\\productionWord\\成品检验记录结果" + ".docx";
                FileOutputStream out = new FileOutputStream(filePath);
                document.write(out);
                out.close();
                System.out.println("Word 表格创建成功！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            num = 1;
        }
        if (num == 1) {
            return "生成失败";
        } else {
            return "/productionWord/成品检验记录结果.docx";
        }


    }


}
